package com.aysafety.iotdataplatformclient.controller.microseism;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aysafety.core.common.DateUtils;
import com.aysafety.core.common.DtoToMapUtils;
import com.aysafety.core.model.MicroseismModel;
import com.aysafety.core.socketdto.ErrorDTO;
import com.aysafety.core.socketdto.MicroseismDTO;
import com.aysafety.iotdataplatformclient.configurer.IotDataPlatformClient;
import com.aysafety.iotdataplatformclient.controller.base.BaseController;
import com.aysafety.iotdataplatformclient.microseism.services.MicroseismService;

@RestController
@RequestMapping("/api/microseism")
@ConfigurationProperties("iotnettyserver")
public class MicroseismController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private int port;

    private String host;

    private String clientId;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getclientId() {
        return clientId;
    }

    public void setclientId(String clientId) {
        this.clientId = clientId;
    }

    @Autowired
    MicroseismService microseismService;

    @Autowired
    IotDataPlatformClient iotDataPlatformClient;

    @RequestMapping("/sendData")
    public Map<String, Object> sendData(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        Map<String, Object> sendData = null;
        MicroseismDTO microseismDTO = new MicroseismDTO(this.clientId);
        try {
            int page = 0;
            int start = 0;
            String dataString = params.get("data").toString();
            MicroseismModel data = microseismService.transformData(dataString);
            microseismDTO.setData(data);
            sendData = DtoToMapUtils.DTOToMap(microseismDTO);
            sendData.put("page", page);
            sendData.put("start", start);
            sendData.put("clientId", this.clientId);
            logger.info(DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss") + ".:." + String.valueOf(page) + ".:."
                + String.valueOf(start));
        } catch (Exception e) {
            logger.error("数据发送异常:", e);
            ErrorDTO errorDTO = new ErrorDTO(this.clientId);
            errorDTO.setDataType("Exception");
            errorDTO.setData(e.toString());
            sendData = DtoToMapUtils.DTOToMap(errorDTO);
        }
        iotDataPlatformClient.setParams(this.getHost(), this.getPort());
        iotDataPlatformClient.sendData(sendData);
        return this.formatReturnData(sendData);
    }
}
